// import 'package:enjoy_plus_hm/pages/home/index.dart';
import 'package:enjoy_plus_hm/pages/house/house_form.dart';
import 'package:enjoy_plus_hm/pages/login/index.dart';
import 'package:enjoy_plus_hm/pages/tab_bar.dart/index.dart';
import 'package:enjoy_plus_hm/utils/token.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'pages/home/detail.dart';
import 'pages/house/building_list.dart';
import 'pages/house/house_detail.dart';
import 'pages/house/house_list.dart';
import 'pages/house/location_list.dart';
import 'pages/house/room_list.dart';
import 'pages/profile/index.dart';
import 'store/counter.dart';

void main() {
  runApp(

      /// MultiProvider widget用于提供多个Provider实例到widget树中
      /// providers: Provider列表，用于管理应用状态
      ///   包含ChangeNotifierProvider，用于创建和管理CounterModel实例
      ///   当CounterModel状态发生变化时，会通知监听者进行UI更新
      MultiProvider(
          providers: [
        ChangeNotifierProvider(create: (context) => CounterModel()),
      ],
          child: MaterialApp(
            // 修改后的安卓
            // home: HomePage(),
            routes: {
              // 路由指向底部导航栏
              '/': (context) => const TabBarPage(),
              // 路由指向详情页
              '/detail': (context) => const NoticeDetail(),
              // 登录页面
              '/login': (context) => const LoginPage(),
            },
            // 路由拦截---token鉴权
            onGenerateRoute: (settings) {
              // print(settings);     RouteSettings("/profile", null)

              final tokenManager = TokenManager();
              final token = tokenManager.getToken() ?? ''; //不支持异步
              // 获取当前token，若为空且目标页面不是登录页，则跳转至登录页；
              if (token.isEmpty && settings.name != '/login') {
                return MaterialPageRoute(
                    builder: (context) => const LoginPage());
              }
              // 有token直接放行
              if (settings.name == '/profile') {
                // print(settings.arguments);
                /// 创建并返回一个 MaterialPageRoute 对象，用于导航到 ProfilePage 页面
                /// 该函数构建一个页面路由，将当前上下文和用户信息参数传递给 ProfilePage
                ///
                /// 参数:
                ///   settings - 包含路由信息和参数的对象，其中 arguments 属性包含传递给页面的用户信息数据
                ///
                /// 返回值:
                ///   MaterialPageRoute - 配置好的页面路由对象，用于页面跳转
                return MaterialPageRoute(
                    builder: (context) =>
                        ProfilePage(userInfo: settings.arguments as Map));
              }
              // 跳转至房屋列表
              if (settings.name == '/house_list') {
                return MaterialPageRoute(
                    builder: (context) => const HouseList());
              }
              // 跳转至地址单元列表
              if (settings.name == '/location_list') {
                return MaterialPageRoute(
                    builder: (context) => const LocationList());
              }
              // 跳转至楼栋列表
              if (settings.name == '/building_list') {
                return MaterialPageRoute(
                    builder: (context) => BuildingList(
                        point: (settings.arguments as Map)['point']));
              }

              // 跳转至房间列表
              if (settings.name == '/room_list') {
                // 获取参数
                final arguments = settings.arguments as Map;
                return MaterialPageRoute(
                    builder: (context) => RoomList(
                        point: arguments['point'],
                        building: arguments['building']));
              }
              if (settings.name == '/house_form') {
                // 获取参数
                final arguments = settings.arguments as Map;
                return MaterialPageRoute(
                    builder: (context) => HouseForm(
                          houseDetail: arguments,
                        ));
              }
              // 跳转至房屋详情
              if (settings.name == '/house_detail') {
                // 获取参数
                final arguments = settings.arguments as Map;
                return MaterialPageRoute(
                    builder: (context) => HouseDetail(houseDetail: arguments));
              }

              // 404页面
              return null;
            },
            // 初始路由
            initialRoute: '/',
          )));
}
